import { PropertyRow } from "./types";
import { PropertyKey } from "@/constants/Enums";
import { modelService } from "@/modules/model/service";
export class CustomPropertyUtil {
  async processProperty(table: PropertyRow[], customUI: any, modelId: string) {
    if (customUI.UIType === "itemflow") {
      // 做到快速调试,不用开启新界面
      // itemflow界面由两个自定义字段和两个原有字段合成 InformationFlow-conveyed 'SysML.ItemFlow.itemProperty',
      // 后续改成接口 childNodeDatas node pool从树中过滤 同类的实现连接器 InformationFlow-realizingConnector
      let cTable = JSON.parse(JSON.stringify(table)) as PropertyRow[];
      let source = cTable.find(x => "InformationFlow-informationSource" === x.key);
      let target = cTable.find(x => "InformationFlow-informationTarget" === x.key);
      if (modelId === customUI.modelIds[1]) {
        [source, target] = [target, source];
      }

      let outTable = cTable.filter(x => "SysML.ItemFlow.itemProperty" === x.key);
      outTable.unshift(table.filter(x => "InformationFlow-conveyed" === x.key)[0]);

      // debugger;
      // 统一显示规则

      if (table.length === 0 || table === null) return [];
      let connectorItemsIds = await modelService.getDefaulteValue(
        [{ instanceId: customUI.connectorId, keys: ["A_realizingConnector_informationFlow-informationFlow"] }],
        app.activeProject!.config.id
      );
      let isConnector = customUI.isConnector;
      if (!isConnector) {
        // 不是ibd图，不显示属性
        outTable.pop();
      }
      let dropSelect = table[0];

      dropSelect.displayLevel = 1;
      let direction = JSON.parse(JSON.stringify(dropSelect)) as PropertyRow;

      let sourceNode = await modelService.getModelData(app.activeProject!.config.id, source!.value![0]!.id);
      let targetNode = await modelService.getModelData(app.activeProject!.config.id, target!.value![0]!.id);
      if (sourceNode.metaclass === "Port") {
        sourceNode = await modelService.getModelData(app.activeProject!.config.id, sourceNode.ownerId);
      }
      if (targetNode.metaclass === "Port") {
        targetNode = await modelService.getModelData(app.activeProject!.config.id, targetNode.ownerId);
      }

      let sourceType = sourceNode.showAttribute.extraAttribute?.typeName;
      let targetType = targetNode.showAttribute.extraAttribute?.typeName;
      let sourceName = sourceType ? sourceType : sourceNode.name;
      let targetName = targetType ? targetType : targetNode.name;

      direction.name = "方向";
      direction.key = PropertyKey.ItemFlow_direction;
      direction.value = modelId === customUI.modelIds[0] ? "0" : "1";
      // 显示规则，存在类型取类型 没有类型取名称
      // 编辑状态 ，切换到非新建后再切换回来总是到第一个
      direction.items = [
        {
          label: "从" + sourceName + "到" + targetName,
          value: "0",
          icon: ""
        },
        {
          label: "从" + targetName + "到" + sourceName,
          value: "1",
          icon: ""
        }
      ];
      let allitemflow = JSON.parse(JSON.stringify(dropSelect)) as PropertyRow;
      allitemflow.name = "项流";
      allitemflow.key = PropertyKey.ItemFlow_name;
      allitemflow.value = modelId;
      if (customUI.modelIds.includes(modelId)) {
        allitemflow.items = [
          {
            label: "新建",
            value: modelId,
            icon: ""
          }
        ];
      } else {
        allitemflow.items = [
          {
            label: "新建",
            value: customUI.modelIds[0],
            icon: ""
          }
        ];
      }

      let ids = connectorItemsIds.data![0].properties[0]?.value as string[];

      let allItemflowQuery = await modelService.modelAdvanceSearch(
        app.activeProject!.config.id,
        "SysML::PortsAndFlows::ItemFlow",
        "baseSt"
      );
      allItemflowQuery.forEach((nodeData: any) => {
        if (ids.includes(nodeData.id)) {
          if (customUI.modelIds.includes(nodeData.id) === false) {
            let disName = `${nodeData.modelTypeCnName}${nodeData.name ? " : " + nodeData.name : ""} [ ${
              nodeData.showAttribute?.sourceName || ""
            } → ${nodeData.showAttribute?.targetName || ""} ]`;
            allitemflow.items.push({
              label: disName,
              value: nodeData.id,
              icon: ""
            });
          }
        }
      });
      // allItemflowQuery  direction 这两栏的切换事件需要重新定义
      outTable.unshift(direction);
      outTable.unshift(allitemflow);

      return outTable;
    } else {
      //
      return table;
    }
  }
}
export const customPropertyUtil = new CustomPropertyUtil();
